<?php

/**
 * 生成user_key
 */
if (!function_exists('make_user_key')) {

    function make_user_key($user_id) {
        return md5(USER_KEY_SALT.$user_id.USER_KEY_SALT);
    }

}
/**
 * 数据整理
 * 
 */
if (!function_exists('json_output')) {
	
	function verify_result($data) {
		if(!$data) return $data;
		foreach ($data as $key => &$val){
			if(is_array($val) && $val){
				$val = verify_result($val);
			}else{
				// if(!trim($val)){
                if(is_array($val) || !trim($val)){
					$val = '';
				}
			}
		}
		return $data;
	}
}

/**
 * 输出json
 */
if (!function_exists('json_output')) {

    function json_output($array = array(), $debug = FALSE) {
    	if ($debug) {
    		echo "<pre>";
    		var_dump($array);
    		echo "</pre>";
            exit();
    	} else {
    		$CI = & get_instance();
    		$CI->load->view("templates/error.php", $array);
    		$CI->output->_display($CI->output->get_output());
            //echo json_encode($array);
            exit();
    	}
    }

}

/**
 * 错误提示
 */
if (!function_exists('error_output')) {

    function error_output($error_tip, $debug = FALSE) {
    	$result = array(
    		'status' => '0',
    		'errorTip' => $error_tip,
    	);

    	json_output($result, $debug);
    }

}

/**
 * CURL GET方式获取数据
 */
if (!function_exists('curl_get')) {

    function curl_get($url, $params = array(), $data_type = '') {
        $CI = & get_instance();
        $url = $url . ($params ? '?' . http_build_query($params, NULL, '&') : '');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
        $res = curl_exec($ch);
        $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $res;
    }

}

/**
 * CURL POST方式获取数据
 */
if (!function_exists('curl_post')) {

    function curl_post($url, $params = array(), $data_type = '') {
        $CI = & get_instance();
        $url = $url;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
        $res = curl_exec($ch);
        $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $res;
    }

}